package com.zhuiyun.project.api.message.service.impl;

import com.zhuiyun.project.api.groupmessage.entity.GroupMessage;
import com.zhuiyun.project.api.groupmessage.mapper.GroupMessageMapper;
import com.zhuiyun.project.api.groupmessage.service.GroupMessageService;
import com.zhuiyun.project.api.message.config.MessageType;
import com.zhuiyun.project.api.message.entity.Message;
import com.zhuiyun.project.api.message.mapper.MessageMapper;
import com.zhuiyun.project.api.message.service.MessageService;
import com.zhuiyun.project.api.messagelist.service.MessageListService;
import com.zhuiyun.project.api.personmessage.entity.PersonMessage;
import com.zhuiyun.project.api.personmessage.mapper.PersonMessageMapper;
import com.zhuiyun.project.api.personmessage.service.PersonMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @ClassName MessageServiceImpl
 * @Description TODO
 * @Author kdj
 * @Date 2023/6/27 14:32
 **/
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private PersonMessageMapper personMessageMapper;
    @Autowired
    private GroupMessageMapper groupMessageMapper;
    @Autowired
    private PersonMessageService personMessageService;


    @Autowired
    private GroupMessageService groupMessageService;

    @Autowired
    private MessageListService messageListService;


    /**
     * @param message
     * @Author kdj
     * @Description 添加消息
     * @Date 2023/6/27 15:49
     * @Return int
     */
    @Override
    public int insertMsg(Message message) {
        //添加
        int i = messageMapper.insertSelective(message);
        if (i > 0) {
            return message.getId();
        }
        return 0;
    }

    /**
     * @param uuid
     * @Author kdj
     * @Description 根据uuid获取子类信息
     * @Date 2023/7/19 17:29
     * @Return java.lang.Object
     */
    @Override
    public Map getMsgSm(String uuid) {
        Example example = new Example(Message.class);
        example.createCriteria().andEqualTo("uuid", uuid);
        //根据uuid获取Message信息
        Message message = messageMapper.selectOneByExample(example);
        Integer messageId = message.getId();
        HashMap<String, Object> map = new HashMap<>();
        switch (message.getType()) {
            case MessageType.PerMsgType:
                //单聊
                //根据messageid获取子的消息对象
                Example perExample = new Example(PersonMessage.class);
                perExample.createCriteria().andEqualTo("messageId", messageId);
                map.put("type", message.getType());
                map.put("data", personMessageMapper.selectOneByExample(perExample));
                return map;
            case MessageType.GroupMsgType: //群聊
                //单聊
                //根据messageid获取子的消息对象
                Example groupExample = new Example(GroupMessage.class);
                groupExample.createCriteria().andEqualTo("messageId", messageId);
                map.put("type", message.getType());
                map.put("data", groupMessageMapper.selectOneByExample(groupExample));
                return map;
            case MessageType.SysMsgType: //单聊
                break;
        }
        return null;
    }

    /**
     * @param id
     * @Author kdj
     * @Description 根据messageId获取小子类
     * @Date 2023/7/19 21:14
     * @Return java.util.Map
     */
    @Override
    public Map getMsgSm(int id) {
        Example example = new Example(Message.class);
        example.createCriteria().andEqualTo("id", id);
        //根据uuid获取Message信息
        Message message = messageMapper.selectOneByExample(example);
        Integer messageId = message.getId();
        HashMap<String, Object> map = new HashMap<>();
        switch (message.getType()) {
            case MessageType.PerMsgType:
                //单聊
                //根据messageid获取子的消息对象
                Example perExample = new Example(PersonMessage.class);
                perExample.createCriteria().andEqualTo("messageId", messageId);
                map.put("type", message.getType());
                map.put("data", personMessageMapper.selectOneByExample(perExample));
                return map;
            case MessageType.GroupMsgType: //单聊
                Example groupExample = new Example(GroupMessage.class);
                groupExample.createCriteria().andEqualTo("messageId", messageId);
                map.put("type", message.getType());
                map.put("data", groupMessageMapper.selectOneByExample(groupExample));
                return map;
            case MessageType.SysMsgType: //单聊
                break;
        }
        return null;
    }

    /**
     * @param uuid
     * @param readId
     * @Author kdj
     * @Description 修改阅读状态
     * @Date 2023/7/19 19:14
     * @Return void
     */
    @Override
    public void updateRead(String uuid, int readId) {
        Map msgSm = this.getMsgSm(uuid);
        Object data = msgSm.get("data");
        switch (Integer.parseInt(msgSm.get("type").toString())) {
            case MessageType.PerMsgType://单聊
                PersonMessage personMessage = (PersonMessage) data;
                //修改消息列表
                personMessageMapper.upIsRead(personMessage.getId());
                messageListService.upMsgListUmReadZero(personMessage.getFromId(), personMessage.getToId(), MessageType.PerMsgType, 0, false);
                break;
            case MessageType.GroupMsgType: //群聊
                break;
            case MessageType.SysMsgType: //系统
                break;
        }
    }

    /**
     * @param uuid
     * @param fromId
     * @Author kdj
     * @Description 删除消息
     * @Date 2023/7/20 14:36
     * @Return void
     */
    @Override
    public void delMsg(String uuid, int fromId,int chatType) {
        Map msgSm = this.getMsgSm(uuid);
        Object data = msgSm.get("data");
        switch (Integer.parseInt(msgSm.get("type").toString())) {
            case MessageType.PerMsgType://单聊
                PersonMessage personMessage = (PersonMessage) data;
                //修改消息列表
                personMessageService.upDelMsg(personMessage,fromId);
                //更改messageList的last消息
                messageListService.upMsgListLastInfo(personMessage.getFromId(), personMessage.getToId(),MessageType.PerMsgType,personMessage.getId(),fromId);
                messageListService.upMsgListUmReadZero(personMessage.getFromId(), personMessage.getToId(), MessageType.PerMsgType, 0, false);
                break;
            case MessageType.GroupMsgType: //群聊
                GroupMessage groupMessage = (GroupMessage) data;
                //修改消息列表
                groupMessageService.upDelMsg(groupMessage,fromId,chatType);
                if(chatType!=MessageType.socketSysNum){
                    messageListService.upMsgListLastInfo(groupMessage.getToId(), groupMessage.getFromId(),MessageType.GroupMsgType,groupMessage.getId(),fromId);
                    messageListService.upMsgListUmReadZero(groupMessage.getToId(), groupMessage.getFromId(),MessageType.GroupMsgType, 0, false);
                }
               break;
            case MessageType.SysMsgType: //系统
                break;
        }
    }
}
